/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.HTMLUIcomponents;

import java.util.ArrayList;

import model.DHTMLConstants;
import model.Labels;
import model.Vocabulary;

import org.apache.commons.lang.StringEscapeUtils;
import org.openrdf.model.URI;

import controller.HelperFunctions;
import controller.elementMapper.Mapper;
import controller.elementMapper.PropertyMapper;
import controller.sesameDataConnector.AbstractDataConnector;


public class Range extends AbstractComponent {
	

	private static final String CSSIDFIELD = "divrange";
	public static final String CSSIDCONTENT = "divrangecontent";
	public static final String NAMEFORMELEMENT = "rangeEntry";
	
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInViewMode(  URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {
		String ret = "";

		ret += getTitleContainer( Labels.RANGE, DHTMLConstants.CLASS_VIEWTYPE, true, true );
		ret += getFormContainer( getFormInViewMode( uri, showNavigation, showNavigationInEditMode ), CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInEditMode(  URI uri, ArrayList<URI> entryList, ArrayList<Exception> exceptions, boolean showNavigation, boolean showCreateLinks ) throws Exception {
		String ret = "";
		
		ret += getTitleContainer( Labels.RANGE, DHTMLConstants.CLASS_VIEWTYPE, true, true );
		ret += getFormContainer( getFormInEditMode( uri, entryList, exceptions, showNavigation, showCreateLinks ), CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	
	public static String getFormInViewMode( URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {

		String ret = "";		

		String entry = "";
		String content = NOTHINGSELECTED;
		
		java.util.ArrayList<URI> list = PropertyMapper.selectRange(uri);	
		content = getElementsHTMLList( list );	

		ret = renderViewForm( 
				content, 
				Mapper.selectHasVersionDateTimeString(uri),
				CSSIDCONTENT, 
				showNavigation, 
				showNavigationInEditMode );
		
		return ret;
	}
	
	

	
	public static String getFormInEditMode( URI uri, ArrayList<URI> entryList, ArrayList<Exception> exceptions, boolean showNavigation, boolean showCreateLinks ) throws Exception {
		
		String ret = "";

		String actualVersionContent = NOTHINGSELECTED;
		String actualTimestamp = Mapper.selectHasVersionDateTimeString(uri);
		
		if( uri!=null ) {

			java.util.ArrayList<URI> list = PropertyMapper.selectRange(uri);
			
			if( list!=null ) {				
				actualVersionContent = getElementsHTMLList( list );		
			}
						
		}
		
		String content = "";
		boolean showInCreationMode = false;
		if( uri==null )
			showInCreationMode = true;
		
		content = getRangeSelectBox( uri, NAMEFORMELEMENT, entryList, DHTMLConstants.CLASSESBELONGINGTOOM, showInCreationMode, showCreateLinks );	

		
		ret = renderEditForm( 
				Labels.RANGE, 
				content,
				exceptions, 
				actualVersionContent, 
				actualTimestamp,
				CSSIDCONTENT, 
				NAMEFORMELEMENT, 
				showNavigation );


		return ret;
	}
	
	
	

	protected static String getRangeSelectBox( URI uri, String nameFormElement, ArrayList<URI> entryList, int ajaxActionCode, boolean showInCreationMode, boolean showCreateLinks ) throws Exception {

		String ret = "";
		
		final URI type = AbstractDataConnector.getType(uri);
		

		if( entryList==null ) {
			entryList=new ArrayList<URI>();

			if ( type==null || type.equals( Vocabulary.DATATYPEPROPERTY ) ) {
					
				entryList.add( Vocabulary.XMLSSTRING ); // default if nothing is selected...
			}
			
		}

		if( showInCreationMode==true ) {
			
			String disableDatatypes = "";
			// String disableObjects = "";
			String checkedRadioDatatypes = "";
			String checkedRadioObjects = "";
			String visibilityDatatypes = "";
			String visibilityObjects = "";
			if( type == null || type.equals( Vocabulary.DATATYPEPROPERTY ) ) {

				checkedRadioDatatypes = "checked=\"checked\"";
				visibilityDatatypes = "visibility:visible";		
				visibilityObjects = "visibility:hidden";	
			}
			else {
				
				checkedRadioObjects = "checked=\"checked\"";
				disableDatatypes = "disabled=\"true\"";
				visibilityDatatypes = "visibility:hidden";		
				visibilityObjects = "visibility:visible";	
			}

			String datatypesPanelId = nameFormElement + "_panel1";
			String classPanelId = nameFormElement + "_panel2";
			
			
			ret +=
				"<input type=\"radio\" name=\"" + nameFormElement + "_radio\" value=\"datatypes\" " + checkedRadioDatatypes + " onclick=\"checkRadioDatatypes('" + classPanelId + "','" + nameFormElement + "' );\"/> Datatypes<br/>";
			//  document.getElementsByName('" + nameFormElement + "')[0].value=this.options[this.selectedIndex].value
			

			
			ret += "<div id=\"" + datatypesPanelId + "\" style=\"" + visibilityDatatypes + "; margin-left:25px;\">";
			
			ret +=
				"	<select name=\"" + nameFormElement + "_sel\" size=\"1\" " + disableDatatypes + " onChange=\"document.getElementsByName('" + nameFormElement + "')[0].value=this.options[this.selectedIndex].value;\" >\n";

			
			ArrayList<model.dataContainer.DataTypeContainer> array2 = controller.HelperFunctions.getDatatypeArray();
			if( array2!=null ) {
				for (int i = 0; i < array2.size(); i++) {
					
					ret += 
				"		<option value=\""+array2.get(i).getId()+"\"";
					if( entryList.get(0).equals(array2.get(i).getId()) ) {
						ret += " selected=\"selected\" ";
					}					
					ret += ">" + StringEscapeUtils.escapeHtml( array2.get(i).getLabel() ) + "</option>\n";
				}
			}
			

			/*
			ret += 
				"		<option value=\"\"";
			if( isopfield ) {
				ret += " selected=\"selected\" ";
			}			
			ret += ">Instances</option>\n";
			*/
			
			
			ret +=
				"	</select><br/>\n";

			ret += "</div>";
			
			
			
			ret +=
				"<input type=\"radio\" name=\"" + nameFormElement + "_radio\" value=\"objects\" " + checkedRadioObjects + " onclick=\"checkRadioObjectTypes('" + classPanelId + "', '" + nameFormElement + "')\" /> " + Labels.INSTANCE.getNamePlural() + " of following " + Labels.CLASS.getNamePlural() + "<br/>";	

			ret += "<div id=\"" + classPanelId + "\" style=\"" + visibilityObjects + "; margin-left:25px;\">";
			
			/*
			String entry = defaultEntry;
			if( entry.equals( Vocabulary.XMLSSTRING.stringValue()) ) {
				entry = "";
			}
			*/

			String createNewAdditionalParams = PropertiesHavingRangeClass.NAMEFORMELEMENT + "=" + HelperFunctions.getUriForHref( uri );
			
			ret += getItemsEditPanel( NAMEFORMELEMENT, entryList, DHTMLConstants.CLASS_VIEWTYPE, createNewAdditionalParams, showCreateLinks );
			
			ret += "</div>";
				
		}
		else {
			// show not in creation but in edit mode...			

			// type MUST not be null!
			if ( type.equals( Vocabulary.DATATYPEPROPERTY ) ) {
				
				// this is an DatatypeProperty...

				ret +=
					"	<select name=\"" + nameFormElement + "_sel\" size=\"1\" onChange=\"document.getElementsByName('" + nameFormElement + "')[0].value=this.options[this.selectedIndex].value;\" class=\"rightselectbox\" >\n";

				
				ArrayList<model.dataContainer.DataTypeContainer> array2 = controller.HelperFunctions.getDatatypeArray();
				if( array2!=null ) {
					for (int i = 0; i < array2.size(); i++) {
						
						ret += 
					"		<option value=\""+array2.get(i).getId()+"\"";
						
						if( entryList.get(0).equals(array2.get(i).getId()) ) {
							ret += " selected=\"selected\" ";
						}
						
						ret += ">" + StringEscapeUtils.escapeHtml( array2.get(i).getLabel() ) + "</option>\n";
					}
				}
				
				ret +=
					"	</select><br/>\n";
				
				
				// the actual values (again; this field is filled / decreased with DHTML but has to be "initialized")
				ret +=
					"<input type=\"hidden\" name=\"" + nameFormElement + "\" value=\"" + HelperFunctions.concatURIsReachable( entryList ) + "\" />\n";
			}
			else {
				// this is an ObjectProperty...

				String createNewAdditionalParams = NAMEFORMELEMENT + "=" + HelperFunctions.getUriForHref( uri );
				
				ret += getItemsEditPanel( NAMEFORMELEMENT, entryList, DHTMLConstants.CLASS_VIEWTYPE, createNewAdditionalParams, showCreateLinks );
				
			}
		}

		return ret;
		
	}

}
